//
//  MineLoginController.m
//  ArtDog
//
//  Created by dllo on 16/8/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MineLoginController.h"
#import "CustomTextFiled.h"
#import "ReminderView.h"
#import "MineRegisterController.h"
#import "ZWYNetTool.h"
#import "NSString+Common.h"
#import "MineHomepageViewController.h"
#import "MineHomepageModel.h"
#import "HLMacro.h"

@interface MineLoginController ()

@property (weak, nonatomic) IBOutlet CustomTextFiled *phoneNumberText;
@property (weak, nonatomic) IBOutlet CustomTextFiled *passwordText;

@end

@implementation MineLoginController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.isFreeKeyboard = YES;
    _phoneNumberText.placeholder = @"手机号";
    _passwordText.placeholder = @"密码";
    _phoneNumberText.leftImage = [UIImage imageNamed:@"icon_user"];
    _passwordText.leftImage = [UIImage imageNamed:@"icon_password"];
    
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)loginAction:(id)sender {
    if (_phoneNumberText.textField.text.length != 11) {
        [ReminderView reminderWithSuperView:sender andTitle:@"请输入11位有效的手机号!" andPosition:HLAlertViewPositionUp];
    } else if (_passwordText.textField.text.length == 0) {
        [ReminderView reminderWithSuperView:sender andTitle:@"请输入密码!" andPosition:HLAlertViewPositionUp];
    } else if ((_passwordText.textField.text.length > 0 && _passwordText.textField.text.length < 6) || _passwordText.textField.text.length > 20) {
        [ReminderView reminderWithSuperView:sender andTitle:@"密码最小长度为6~20位!" andPosition:HLAlertViewPositionUp];
    } else {
        NSString *urlString = [@"http://api.artgoer.cn:8084/artgoer/api/v1/login?" stringByAppendingString:_phoneNumberText.textField.text];
        NSString *paramString = [[[@"mobileNo=" stringByAppendingString:_phoneNumberText.textField.text]stringByAppendingString:@"&password="]stringByAppendingString:[_passwordText.textField.text stringWith32BitMD5Lower]];
        [ZWYNetTool POST:urlString andBody:paramString andBodyStyle:ZWYBodyString andHeader:@{@"Host": @"api.artgoer.cn:8084"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
            [ReminderView reminderWithSuperView:sender andTitle:result[@"message"] andPosition:HLAlertViewPositionUp];
            if ([result[@"status"] isEqualToString:@"200"]) {
                MineHomepageViewController *homePageVC = [[MineHomepageViewController alloc]initWithNibName:@"MineHomepageViewController" bundle:[NSBundle mainBundle]];
                homePageVC.homePageModel = [MineHomepageModel modelWithDic:result[@"data"]];
                [[NSUserDefaults standardUserDefaults]setObject:@{@"id": [NSString stringWithFormat:@"%@", homePageVC.homePageModel.idField], @"token": homePageVC.homePageModel.token, @"phoneNumber": _phoneNumberText.textField.text, @"password": [_passwordText.textField.text stringWith32BitMD5Lower]} forKey:kUserDefaultPathKey];
                [self.navigationController pushViewController:homePageVC animated:YES];
            }
        } andFailureBlock:^(NSError *error) {
            NSLog(@"%@", error);
        }];

    }

}

- (IBAction)forgetPassword:(id)sender {
    MineRegisterController *forgetVC = [[MineRegisterController alloc]initWithNibName:@"MineRegisterController" bundle:[NSBundle mainBundle]];
    forgetVC.isForgetVC = YES;
    
    [self.navigationController pushViewController:forgetVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
